package com.jzr.medical.common;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jzr.common.util.U;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public interface ILayService<T extends LayEntity<K>,V extends T,K extends Serializable> extends IService<T> {
    V getByIdV(Serializable id);
    List<V> list(IPage<V> page,QueryWrapper<V> wrapper);
    List<V> list(QueryWrapper<V> wrapper);
    V getOne(QueryWrapper<V> wrapper);
    List<V> listByIdsV(Collection<? extends Serializable> idList);
    List<V> listByMap(IPage<V> page,Map<String, Object> columnMap);
    List<V> listByMapV(Map<String, Object> columnMap);
    K getId(Wrapper wrapper);
    List<K> getIdList(Wrapper wrapper);

    default V saveAndGet(V record){
        if(record.getId() != null){
            return updateAndGet(record);
        }
        if(save(record)){
            return getByIdV(record.getId());
        }else {
            return null;
        }
    }
    default V updateAndGet(V record){
        if(record.getId()==null){
            return saveAndGet(record);
        }
        if(updateById(record)){
            return getByIdV(record.getId());
        }else {
            return null;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    default List<V> saveList(List<V> list) throws Exception {
        List<V> result = new ArrayList<>();
        Integer userId = SecurityHelper.getUserId();
        for(V item: list){
            if(item.getUpdateBy() == null){
                item.setUpdateBy(SecurityHelper.getUserId());
            }
            if(item.getUpdateTime() == null){
                item.setUpdateTime(LocalDateTime.now());
            }
            V tmp = null;
            if(!U.isBlank(item.getId())){
               tmp = updateAndGet(item);
            }else{
               tmp = saveAndGet(item);
            }
            if(tmp != null){
                tmp.setLayTableIndex(item.getLayTableIndex());
                result.add(tmp);
            }
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    default List<V> saveBatch(LayBatch<V> batch){
        List<V> result = new ArrayList<>();
        Integer userId = SecurityHelper.getUserId();

        for(V item: batch.getAdds()){
            if(item.getUpdateBy() == null){
                item.setUpdateBy(userId);
            }
            if(item.getUpdateTime() == null){
                item.setUpdateTime(LocalDateTime.now());
            }
            V tmp = saveAndGet(item);
            if(tmp != null) {
                tmp.setLayTableIndex(item.getLayTableIndex());
                result.add(tmp);
            }
        }
        for(V item: batch.getUpdates()){
            if(item.getUpdateBy() == null){
                item.setUpdateBy(userId);
            }
            if(item.getUpdateTime() == null){
                item.setUpdateTime(LocalDateTime.now());
            }
            V tmp = updateAndGet(item);
            if(tmp != null) {
                tmp.setLayTableIndex(item.getLayTableIndex());
                result.add(tmp);
            }
        }
        for(V item: batch.getDeletes()){
            removeById(item.getId());
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    default List<V> saveBatch(List<V> list){
        LayBatch<V> batch = new LayBatch<>(list);
        return this.saveBatch(batch);
    }
}
